<template>
  <div class="login">
    <div>
      <span class="iconfont iconicon-test"></span>
    </div>
    <div class="news">
      <span class="iconfont iconnew"></span>
    </div>

    <Authinput
      type="text"
      :rule="/^.{2,11}$/"
      errMsg="请输入正确用户名"
      placeholder="用户名 / 手机号码"
      @setValue="setuaername"
    />
    <Authinput
      type="password"
      errMsg="请输入2到11位数字"
      :rule="/^\d{2,11}$/"
      placeholder="密码"
      @setValue="setpassword"
    />
    <router-link to="profile"> 跳转到个人中心</router-link>
    <!-- @click.native监听原生的点击事件 -->
    <Button value="登录" @click.native="login" />
  </div>
</template>

<script>
import Authinput from "../components/Authinput";
import Button from "../components/Button";
// import login from "./loginApi";  !!!一定要解构呀呀呀！！！
import { login } from "./loginApi";
export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  components: {
    Authinput,
    Button,
  },
  methods: {
    setuaername(newVal) {
      this.username = newVal;
    },
    setpassword(newVal) {
      this.password = newVal;
    },
    login() {
      // this.$axios({
      //   method: "post",
      //   url: "/login",
      //   data: {
      //     username: this.username,
      //     password: this.password,
      //   },
      // })
      const data = {
        username: this.username,
        password: this.password,
      };
      // this.login(data)   ！！不要加this！！
      login(data)
        .then((res) => {
          console.log(res.data);
          const { message, data } = res.data;
          if (message === "登录成功") {
            this.$toast.success("登录成功啦");
            // console.log(res.data.data.token);
            localStorage.setItem("token", data.token);
            localStorage.setItem("userId", data.user.id);
            // 跳转页面：
            this.$router.push("/profile");
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  width: 360 / 360 * 100vw;
  padding: 18 / 360 * 100vw;
  // padding-left: 15 / 360 * 100vw;
  // padding-right: 15 / 360 * 100vw;
  background-color: #f2f2f2;

  .iconicon-test {
    font-size: 27 / 360 * 100vw;
  }
  .news {
    // width: 100%;
    text-align: center;
    .iconnew {
      font-size: 126 / 360 * 100vw;
      color: #d81e06;
    }
  }
}
</style>